package com.ddz.library.toast

import android.os.Handler
import android.os.Looper
import android.provider.Settings
import android.widget.Toast
import com.ddz.library.JKLibrary

object JKToast {
    var handler = Handler(Looper.getMainLooper())
    var lastToastTimeMillis = 0L
    var lastMsg: String? = null
    fun show(msg: String?) {
        val currentTimeMillis = System.currentTimeMillis()
        if (!msg.isNullOrBlank() && (lastMsg != msg || (lastMsg == msg && lastToastTimeMillis - currentTimeMillis > 3000))) {
            if (Thread.currentThread() == Looper.getMainLooper().thread) {
                realShow(msg)
                return
            }
            handler.post {
                realShow(msg)
            }
        }
        lastToastTimeMillis = System.currentTimeMillis()
    }

    private fun realShow(msg: String?) {
        Toast.makeText(JKLibrary.context, msg, Toast.LENGTH_SHORT).show()
    }
}